<?php
/**
 * @filesource:					GeneralCustomerAction.class.php
 * @desc:						客户管理模块
 * 
 * @author:						shenyaqing
 * @copyright:					hou.yining@gmail.com
 * @date:						2011-04-19
 */
class GeneralCustomerAction extends CommonAction{
	public function ___initialize(){
		$obj = D('admin_user');
		$result = $obj->where()->select();
		return $result;
	}
	public function index(){
					$crumb_info = array(
						array('uri' => '__URL__', 'title' => L('conper_manage'), 'sort' => C('CRUMB_LAST')),
					);
					$this->_mergeCrumbInfo($crumb_info);
					$this->_action_link = array(
						array('uri' => '__URL__/add', 'title' => L('add_conper')),
					);
					$this->assign('crumb_info', $this->_crumb_info);
					$this->assign('action_link', $this->_action_link);
					
					$name=$this->getActionName();
					$model = D ($name);
//					$id = $_REQUEST['id'];
//					$map = "type_id='$id'";
					if (! empty ( $model )) {
						$list = $this->_list ( $model);
					}
					$this->display ();

		}

	
	public function add(){
		$vol = $this->___initialize();
		$this->assign('vol',$vol);
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('conper_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/add', 'title' => L('add_conper'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index/id/1', 'title' => L('list_conper')),
			);
		$omit = D('region');
		$province = $omit->where("level=1")->select();
		$this->assign('province',$province);
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::add();
	}
	public function edit(){
		$vol = $this->___initialize();
		$this->assign('vol',$vol);
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('conper_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/edit/id/' . $_REQUEST['id'], 'title' => L('edit_conper'), 'sort' => C('CRUMB_LAST')),
		);

		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index/id/1', 'title' => L('list_conper')),
			);
		$omit = D('region');
		$province = $omit->where("level=1")->select();
		$this->assign('province',$province);
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::edit();
	}
	public function search(){
		$model = M ( 'general_customer' );
		//$result = parent::_search();
		$where = $_POST['search'];
		$result = $model->where("name='$where'")->select(); 
		$this->assign('list',$result);
		$this->display('index');
	}
	public function all_delete(){
    	$obj = D('general_customer');
		$zhi = $_GET['id']; 
		//var_dump($zhi);
		//echo strlen($zhi);die;
			//echo $zhi;  
	    $zhi=explode(',',$zhi);
	    //var_dump($zhi);die;
		//在循环删除就可以
		for($i=0;$i<count($zhi);$i++)
		{
		  $obj->query("delete from GMS_general_customer where id='$zhi[$i]'");
		  	 
		} 	
		echo "<script>alert('已全部删除！');window.location.href('".ROOT_PATH."GeneralCustomer/index');</script>";
    }
    //增加联系记录
    public function contactAdd(){
    	$id = $_POST['charger_person_id'];
    	$model = new ChooseContactModel();
		$model->create();
		$list = $model->add();
		if ($list!==false) { //保存成功
			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('新增成功!');
			echo "<script>window.location.href('".ROOT_PATH."GeneralCustomer/view/id/'.$id.'');</script>";
		} else {
			//失败提示
			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->error ('新增失败!');
			echo "<script>window.location.href('".ROOT_PATH."GeneralCustomer/view/id/'.$id.'');</script>";
		}
    }
    //增加反馈记录
 	public function feedAdd(){
 		$id = $_POST['charger_person_id'];
    	$model = new FeedbackModel();
		$model->create();
		$list = $model->add();
		if ($list!==false) { //保存成功
			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('新增成功!');
			echo "<script>window.location.href('".ROOT_PATH."GeneralCustomer/view/id/'.$id.'');</script>";
		} else {
			//失败提示
			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->error ('新增失败!');
			echo "<script>window.location.href('".ROOT_PATH."GeneralCustomer/view/id/'.$id.'');</script>";
		}
    }
    //加载增加反馈处理结果记录页面
    public function feedpage(){
    	$this->assign('id',$_REQUEST['id']);
    	$id = $_REQUEST['id'];
    	$model = M('feedback');
    	$charge_person_id = $model->where("id='$id'")->getField('charge_person_id');
    	$this->assign('charge_person_id',$charge_person_id);
    	$this->display();
    }
    //增加或修改反馈处理结果
	public function feedUpdate(){
		$id = $_REQUEST['charge_person_id'];
		$User = new FeedbackModel(); 
		$User->create();
		
		$list = $User->save(); 
		if ($list!==false) { //保存成功
//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('新增/编辑成功!');
			echo "<script>window.location.href('http://localhost/gms/index.php/GeneralCustomer/view/id/'.$id.'');</script>";
			
		} else {
			//失败提示
			//$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->error ('新增/编辑失败!');
			echo "<script>window.location.href('http://localhost/gms/index.php/GeneralCustomer/view');</script>";
		}
	 }
	 //查询预订单信息
    public function bookRequest(){
    	$id = $_REQUEST['id'];
    	$book = new BookRequestModel();
		$bk = $book->getBookRequestById($id);
		$this->assign('bk',$bk);
    	$this->display();
    }
    //查询墓地退够单信息
     public function graveReturn(){
     	$id = $_REQUEST['id'];
    	$gravereturn = M('grave_return_request');
		$gt = $gravereturn->table('GMS_grave_return_request b,GMS_general_customer g,GMS_admin_user a')->where("b.charge_person_id=g.id and b.creater_id=a.id and b.id='$id'")->field('b.*,g.name customer_name,a.account')->find();
		$this->assign('gt',$gt);
    	$this->display();
     }
     //查询墓地销售单信息
     public function graveSale(){
     	$id = $_REQUEST['id'];
    	$gravesale = M('grave_sale_request');
		$sa = $gravesale->table('GMS_grave_sale_request b,GMS_general_customer g,GMS_admin_user a')->where("b.charge_person_id=g.id and b.creater_id=a.id and b.id='$id'")->field('b.*,g.name customer_name,a.account')->find();
		$this->assign('sa',$sa);
    	$this->display();
     }
    //查询墓地退订申请单信息
     public function unbook(){
     	$id = $_REQUEST['id'];
    	$unbook = M('grave_unbook_request');
		$un = $unbook->table('GMS_grave_unbook_request b,GMS_general_customer g,GMS_admin_user a')->where("b.applicant_id=g.id and b.creater_id=a.id and b.id='$id'")->field('b.*,g.name customer_name,a.account')->find();
		$this->assign('un',$un);
    	$this->display();
     }
     //查询付款/退款申请单信息
     public function payment(){
     	$id = $_REQUEST['id'];
    	$payment = M('payment_request');
		$pm = $payment->table('GMS_payment_request b,GMS_general_customer g,GMS_admin_user a')->where("b.charge_person_id=g.id and b.creater_id=a.id and b.id='$id'")->field('b.*,g.name customer_name,a.account')->find();
		$this->assign('pm',$pm);
    	$this->display();
     }
     //查询预留单信息
     public function reserve(){
     	$id = $_REQUEST['id'];
    	$reserve = new ReserveRequestModel();
		$re = $reserve->getReserveRequestById($id);
		$this->assign('re',$re);
    	$this->display();
     }
     //查询选址单信息
     public function choosegrave(){
     	$id = $_REQUEST['id'];
     	$choosegrave = new ChooseGraveModel();
		$cg = $choosegrave->getChooseGraveInfo($id);
		$this->assign('cg',$cg);
		$this->display();
     }
	public function view($name = ''){
	    $name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ( $id );
		//预订单信息
		$book = new BookRequestModel();
		$bk = $book->getBookRequestByApplicant($id);
		//墓地退够单
		$gravereturn = M('grave_return_request');
		$gt = $gravereturn->table("GMS_grave_return_request b,GMS_admin_user a")->where("b.creater_id=a.id and b.charge_person_id='$id'")->field("b.*,a.account creater_name")->find();
		//墓地销售单
		$sale = M('grave_sale_request');
		$sa = $sale->table("GMS_grave_sale_request b,GMS_admin_user a")->where("b.creater_id=a.id and b.charge_person_id='$id'")->field("b.*,a.account creater_name")->find();
		//墓地退订申请单
		$unbook = M('grave_unbook_request');
		$un = $unbook->table("GMS_grave_unbook_request b,GMS_admin_user a")->where("b.creater_id=a.id and b.applicant_id='$id'")->field("b.*,a.account creater_name")->find();
		//墓地付款/退款单
		$payment = M('payment_request');
		$pm = $payment->table("GMS_payment_request b,GMS_admin_user a")->where("b.creater_id=a.id and b.charge_person_id='$id'")->field("b.*,a.account creater_name")->find();
		//预留单
		$reserve = new ReserveRequestModel();
		$re = $reserve->getReserveRequestByApplicant($id);
		dump($re);
		//选址申请单
		$choosegrave = new ChooseGraveModel();
		$cg = $choosegrave->getChooseGraveListByApplicant($id);

		//联系记录
		$contact = M('choose_contact');
		$con = $contact->where("charger_person_id='$id'")->select();

		//投诉与意见
		$feedback = M('feedback');
		$fb = $feedback->where("charge_person_id='$id'")->select();

		$this->assign ( 'vo', $vo );
		$this->assign('creater',$_SESSION['loginUserName']);
		$this->assign('con',$con);
		$this->assign('fb',$fb);
		$this->assign('bk',$bk);
		$this->assign('gt',$gt);
		$this->assign('sa',$sa);
		$this->assign('un',$un);
		$this->assign('pm',$pm);
		$this->assign('re',$re);
		$this->assign('cg',$cg);
		
		
		$this->display ();
	}
	public function selectcity(){
		$id = $_GET['province_id'];
		$city = D('region');
		$town = $city->where("parent_id='$id'")->select();
		exit(json_encode($town));
	}
	public function selectarea(){
		$id = $_GET['city_id'];
		$city = D('region');
		$county = $city->where("parent_id='$id'")->select();
		exit(json_encode($county));
	}

}
?>